import { ts } from "qvog-dsl";

import { rule } from 'qvog-dsl';

const SEVERITY = 2;
const DOC_PATH = 'docs/condition-and-loop-curly.md';
const DESCRIPTION = 'The logical code of conditional statements and loop statements must be written in curly braces, which facilitates code modification.';

export default rule()
    .match((node): node is ts.IfStatement | 
    ts.ForInStatement | 
    ts.ForOfStatement | 
    ts.WhileStatement | 
    ts.DoStatement | 
    ts.ForStatement => {
        return ts.isIfStatement(node) || 
        ts.isForInStatement(node) || 
        ts.isForOfStatement(node) || 
        ts.isWhileStatement(node) || 
        ts.isDoStatement(node) || 
        ts.isForStatement(node);
    })
    .when((node): boolean => {
        for (let child of node.getChildren()) {
            if (ts.isExpressionStatement(child)) {
                return true;
            }
        }
        return false;
    })
    .report({
        severity: SEVERITY,
        description: DESCRIPTION,
        docPath: DOC_PATH,
    });
